package com.spbdx.system.service.mongodb;

import com.spbdx.common.utils.DateUtils;
import com.spbdx.system.domain.BaseMongoEntity;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Component
public class BaseMongoServiceImpl implements IBaseMongoService {

    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 查询所有数据
     *
     * @param query
     * @param t
     * @param <T>
     * @return
     */
    @Override
    public <T extends BaseMongoEntity> List<T> selectList(Query query, Class<T> t) {
        return mongoTemplate.find(query, t);
    }

    @Override
    public <T extends BaseMongoEntity> void save(T t) {
        Date nowDate = DateUtils.getNowDate();
        calendar.setTime(nowDate);
        t.setEt(calendar.getTime());
        t.setCt(nowDate);
        mongoTemplate.save(t);
    }

}
